package com.ipt.frm.pw;

import java.io.IOException;
import java.util.Properties;
import java.util.ResourceBundle;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


/**
 * Servlet implementation class ServletMaestro
 */
public class ServletMaestro extends HttpServlet {
	private static final long serialVersionUID = 1L;

	static Properties propertiesMsg;
	//static final String PATH_PROPERTIES_MSG="/WEB-INF/config/messages.properties";
	
	//parametros de inicio del servlet
	//TODO debe estar en un unico sitio
	static final String INIT_PARAM_USER_NAME = "username";
	
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ServletMaestro() {
        super();        
    }
    
    @Override
    public void init() throws ServletException {
    	super.init();
    	/*
    	propertiesMsg = new Properties();
    	try {
			propertiesMsg.load(getServletContext().getResourceAsStream(PATH_PROPERTIES_MSG));
		} catch (IOException e) {
			e.printStackTrace();
		}
		*/
    }

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		//RequestDispatcher dispacher = null;
				
		if ( session.getAttribute(INIT_PARAM_USER_NAME) != null){
			super.service(request, response);
		
		}else{			 
			 //request.setAttribute("MSG", new Mensaje(TipoMensaje.ERROR,"Debe logearse para poder usar la AppWeb"));
			
			propertiesMsg = (Properties) getServletContext().getAttribute("propertiesMsg");
			request.setAttribute("MSG",new Mensaje(TipoMensaje.ERROR,propertiesMsg.getProperty("servlet.master.no.user")));
			 //dispacher = request.getRequestDispatcher("login.jsp");		 
			 //dispacher.forward(request, response);
			 request.getRequestDispatcher("login.jsp").forward(request, response);
			 
			 //response.sendRedirect("login.jsp");
			 
		}
		
	}
	
	@Override
	public void destroy() {
		super.destroy();
		propertiesMsg = null;		
	}

}
